package com.fitns.user.service.baseConfig;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fitns.user.conf.Constants;
import com.fitns.user.conf.ExcelConstants;
import com.fitns.user.dao.baseConfig.TbGarbageBurnDataDao;
import com.fitns.user.dto.baseConfig.TbGarbageBurnDateDTO;
import com.fitns.user.dto.baseConfig.TbGarbageBurnDateVO;
import com.fitns.user.dto.baseConfig.TbMiddleLedgerDTO;
import com.fitns.user.model.baseConfig.TbGarbageBurnData;
import com.fitns.user.model.baseConfig.TbMiddleLedger;
import com.fitns.user.utils.DateUtil;
import com.github.pagehelper.PageInfo;
import com.xiaoleilu.hutool.lang.Validator;

@Service
public class TbGarbageBurnDataService {
	
	@Autowired
	private TbGarbageBurnDataDao tbGarbageBurnDateDao;
	
	/**  
	 * 新增垃圾焚烧厂数据管理
	 *
	 * */
	@Transactional
	public Integer insertGarbageBurnDate(TbGarbageBurnDateDTO tbGarbageBurnDateDTO){
		TbGarbageBurnData record = new TbGarbageBurnData();
		BeanUtils.copyProperties(tbGarbageBurnDateDTO, record);
		record.setBeginTime(new Date());
		return tbGarbageBurnDateDao.insertSelective(record);
	}

	
	/**
	 * 修改垃圾焚烧厂数据
	 */
	@Transactional
	public Integer updateGarbageBurnDate(TbGarbageBurnDateDTO tbGarbageBurnDateDTO){
		TbGarbageBurnData record = new TbGarbageBurnData();
		BeanUtils.copyProperties(tbGarbageBurnDateDTO, record);
		record.setId(tbGarbageBurnDateDTO.getId());
		record.setBeginTime(new Date());
		Integer update = tbGarbageBurnDateDao.updateByPrimaryKeySelective(record);
		return update;
	}
	
	/**
	 * 删除垃圾焚烧厂数据
	 */
	@Transactional
	public Integer deleteGarbageBurnDate(long id) {
		return tbGarbageBurnDateDao.deleteByPrimaryKey(id);
	}
	
	
	/**
	 *根据id查看垃圾焚烧厂数据
	 */
	@Transactional
	public TbGarbageBurnData selectGarbageBurnDate(long id) {
	 
		TbGarbageBurnData selectById =  tbGarbageBurnDateDao.selectByPrimaryKey(id);
		
		return selectById;
	}
	
	/**
	 *查询
	 */
	public PageInfo<TbGarbageBurnDateVO> selectGarbageBurnDateList(TbGarbageBurnDateDTO tbGarbageBurnDateDTO) {
		tbGarbageBurnDateDTO.pageHandle();
		List<TbGarbageBurnDateVO> select = tbGarbageBurnDateDao.selectGarbageBurnDateList(tbGarbageBurnDateDTO);
		PageInfo<TbGarbageBurnDateVO> selectList = new PageInfo<>(select);
		return selectList;
	}
	
	
	/**
	 * 上传图片
	 * @param tbMiddleLedgerDTO
	 * @return
	 */
	@Transactional
	public Integer inserImage(TbGarbageBurnDateDTO tbGarbageBurnDateDTO) {
		TbGarbageBurnData record = new TbGarbageBurnData();
		BeanUtils.copyProperties(tbGarbageBurnDateDTO, record);
		if (com.xiaoleilu.hutool.lang.Validator.isNull(tbGarbageBurnDateDTO.getId())) {

			return this.insertGarbageBurnDate(tbGarbageBurnDateDTO);
		} else {
			record.setImage(tbGarbageBurnDateDTO.getImage());
			record.setModifyTime(new Date());
			return tbGarbageBurnDateDao.updateByPrimaryKey(record);
		}

	}
	
	
	public List<List<String>> export(TbGarbageBurnDateDTO tbGarbageBurnDateDTO) {
		List<TbGarbageBurnData> facilityDataList = tbGarbageBurnDateDao.searchForExport(tbGarbageBurnDateDTO);
		List<List<String>> excelList = new ArrayList<>();
		excelList.add(this.getRowHeader(ExcelConstants.GARBAGE_BURN_DATA_IMPORT_HEADER.class));
		for (int i = 0; i < facilityDataList.size(); i++) {

			excelList.add(this.getrow(facilityDataList.get(i), i));
		}
		return excelList;
	}

	public List<String> getRowHeader(Class clazz) {
		Field[] fields = clazz.getDeclaredFields();
		List<String> rowHeader = new ArrayList<>();
		for (int i = 0; i < fields.length - 1; i++) {
			try {
				Object object = fields[i].get(clazz);
				String value = object.toString();
				rowHeader.add(value);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return rowHeader;
	}

	public List<String> getrow(TbGarbageBurnData tbGarbageBurnData, int i) {
		List<String> row = new ArrayList<>();

		row.add(String.valueOf(i + 1));
		// if else处理的是数据库要导出的数据中不为string的字段，
		// 时间格式的就是这样的写法，因为工具类中已经对时间处理了，固定 的写法
		row.add(tbGarbageBurnData.getName());
		row.add(tbGarbageBurnData.getStatus());
		row.add(tbGarbageBurnData.getAcreage());
		row.add(tbGarbageBurnData.getCounter());
		if (Validator.isNull(tbGarbageBurnData.getPopulation())) {
			row.add("");
		} else {
			row.add(String.valueOf(tbGarbageBurnData.getPopulation()));
		}
		row.add(tbGarbageBurnData.getLiabler());
		
		row.add(tbGarbageBurnData.getPhone());
		row.add(tbGarbageBurnData.getTerminal());
		row.add(tbGarbageBurnData.getRfid());
		row.add(tbGarbageBurnData.getDescs());
		row.add(tbGarbageBurnData.getImage());
		if (Validator.isNull(tbGarbageBurnData.getBeginTime())) {
			row.add("");
		} else {
			row.add(DateUtil.parseDateToStr(tbGarbageBurnData.getBeginTime(), Constants.DATESTR));
		}
		row.add(tbGarbageBurnData.getAddress());
		return row;
		
	}
}
